using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Blople.GameComponents
{
    public class TextMenu : Microsoft.Xna.Framework.DrawableGameComponent
    {
        protected Texture2D menuTexture;
        protected SpriteFont font;
        protected Vector2 position = new Vector2(50, 150);

        protected int selectedIndex = 0;
        private readonly List<String> menuItems;

        KeyboardState oldKeyboardState;

        public TextMenu(Game game, SpriteFont font, Texture2D menuTexture) : base(game)
        {
            this.font = font;
            this.menuTexture = menuTexture;
            menuItems = new List<String>();
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            for (int i = 1; i <= 60; i++)
            {
                if (i == 60)
                {
                    bool down, up;

                    KeyboardState keyBoardState = Keyboard.GetState();

                    down = oldKeyboardState.IsKeyDown(Keys.Down) && keyBoardState.IsKeyUp(Keys.Down);
                    up = oldKeyboardState.IsKeyDown(Keys.Up) && keyBoardState.IsKeyUp(Keys.Up);

                    if (down)
                    {
                        selectedIndex++;
                        if (selectedIndex == menuItems.Count)
                        {
                            selectedIndex = 0;
                        }
                    }

                    if (up)
                    {
                        selectedIndex--;
                        if (selectedIndex < 0)
                        {
                            selectedIndex = menuItems.Count - 1;
                        }
                    }

                    oldKeyboardState = keyBoardState;

                    base.Update(gameTime);
                }
            }
        }

        public override void Draw(GameTime gameTime)
        {
            Color theColor;
            SpriteBatch sBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            float y = position.Y;

            for (int i = 0; i < menuItems.Count; i++)
            {
                if (i == SelectedIndex)
                {
                    theColor = new Color(118, 58, 0);
                }
                else
                {
                    theColor = Color.White;
                }
                sBatch.Draw(menuTexture, new Vector2(position.X, y), Color.White);
                sBatch.DrawString(font, menuItems[i], new Vector2(position.X + 27, y + 27), Color.Gray);
                sBatch.DrawString(font, menuItems[i], new Vector2(position.X + 25, y + 25), theColor);
                y += 120;
            }

            base.Draw(gameTime);
        }

        public void AddMenuItem(string item)
        {
            menuItems.Add(item);
        }

        public int SelectedIndex
        {
            get { return selectedIndex; }
            set { selectedIndex = value; }
        }

        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }
    }
}